<template>
  <div>
    <div class="input-box"  v-if="type==='text'">
      <div class="label"><slot name="label">{{ label }}</slot></div>
      <div class="input" :class="{errors:rule}">
        <div class="left" v-if="leftIcon">
          <slot name="left-icon"><img src="@/assets/images/code_icon.png" alt=""></slot>
        </div>
        <input
            v-if="!disabled"
            v-bind="$attrs"
            @input="handleInput"
            type="text"
            :placeholder="placeholder">
        <input
            readonly="readonly"
            v-else
            v-bind="$attrs"
            @input="handleInput"
            type="text"
            :placeholder="placeholder">
      </div>
      <div class="error" v-if="rule">
        {{ ruleLabel }}
      </div>
    </div>


    <div class="input-box"  v-if="type==='password'">
      <div class="label"><slot name="label">{{ label }}</slot></div>
      <div class="input" :class="{errors:rule}">
        <div class="left" v-if="leftIcon">
          <slot name="left-icon"><img src="@/assets/images/code_icon.png" alt=""></slot>
        </div>
        <input v-if="!disabled" v-bind="$attrs" @input="handleInput" :type="text ? 'text' : 'password'" :placeholder="placeholder">
        <input v-else v-bind="$attrs" readonly="readonly" @input="handleInput" :type="text ? 'text' : 'password'" :placeholder="placeholder">
        <div class="right-ipt">
          <img @click="text=false" v-if="text" src="@/assets/images/open.png" alt="">
          <img @click="text=true" v-else src="@/assets/images/close.png" alt="">
        </div>
      </div>
      <div class="error" v-if="rule">
        {{ ruleLabel }}
      </div>
    </div>


    <div class="input-box"  v-if="type==='code'">
      <div class="label"><slot name="label">{{ label }}</slot></div>
      <div class="input" :class="{errors:rule}">
        <div class="left" v-if="leftIcon">
          <slot name="left-icon"><img src="@/assets/images/code_icon.png" alt=""></slot>
        </div>
        <input
            v-bind="$attrs"
            @input="handleInput"
            type="text"
            :placeholder="placeholder">
        <div class="right" @click="$emit('click')" v-if="qrCode">
          <slot name="qrcode">
            <img :src="qrCode" alt="">
          </slot>
        </div>
      </div>
      <div class="error" v-if="rule">
        {{ ruleLabel }}
      </div>
    </div>


    <div class="input-box"  v-if="type==='phone'">
      <div class="label"><slot name="label">{{ label }}</slot></div>
      <div class="input" :class="{errors:rule}">
        <div class="left-phone" @click="getNum">
          <span>
            + {{ getCode.countryCode }} <b><van-icon name="arrow-down" /></b>
          </span>
          <div class="left-nav" v-if="isNum">
            <div class="jt"/>
            <div class="menu-box">
              <div v-for="(item,index) in telList" @click="changePhone(item)" :key="index" class="box-nav">
                <span>{{item.countryNameEnglish}}</span>
                <span>{{ item.countryCode }} </span>
              </div>
            </div>
          </div>
        </div>
        <input
            v-bind="$attrs"
            @input="handleInputs"
            type="text"
            :placeholder="placeholder">
      </div>
      <div class="error" v-if="rule">
        {{ ruleLabel }}
      </div>
    </div>

  </div>
</template>

<script>

  import store from "@/store";

  export default {
    props:{
      disabled:{
        default:false
      },
      type:{
        default:'text'
      },
      label:{
        default: ''
      },
      placeholder:{
        default:''
      },
      rule:{
        default:false
      },
      ruleLabel:{
        default:''
      },
      tel:{
        default:''
      },
      leftIcon:{
        default:''
      },
      code:{
        default:''
      }
    },
    data(){
      return{
        inputValue:'',
        text:false,
        qrCode:'',
        isNum:false,
        getCode:{},
        telList:[]
      }
    },
    methods:{
      handleInput(event) {
        this.inputValue = event.target.value;
        this.$emit('input', this.inputValue);
      },
      handleInputs(event){
        this.inputValue = event.target.value;
        this.$emit('input', this.inputValue);
      },
      getNum(){
        this.isNum = !this.isNum
      },
      changePhone(item){
        this.getCode = item
        this.$emit('phone',item.countryCode)
      },
    },

    watch:{
      tel(newValue,oldValue){
        if (this.type==='phone'){
          this.getCode = this.tel ? this.tel[0] : {}
        }
      },
      code(newValue,oldValue){this.qrCode = newValue}
    },

    mounted() {
      if (this.type==='phone'){
        if (this.$store.state.countryCode){
          this.telList = this.$store.state.countryCode
          this.getCode = this.telList.length>0 ? this.telList[0] : {}
          this.$emit('phone',this.getCode.countryCode)
        }else{
          store.dispatch('getCountryCode').then(res=>{
            this.telList = res.data
            this.getCode = this.telList.length>0 ? this.telList[0] : {}
            this.$emit('phone',this.getCode.countryCode)
          }).catch(err=>{})
        }
      }
    }
  }
</script>

<style scoped lang="scss">
  .input-box{
    width: 100%;
    margin-bottom: 15px;
    display: flex;
    flex-wrap: wrap;
    .label{
      padding: 0 10px;
      height: 18px;
      background: #FFFFFF;
      box-shadow: 0px 4px 4px 0px rgba(160,58,58,0.25);
      border-radius: 10px 10px 10px 10px;
      font-weight: 500;
      font-size: 12px;
      color: #242B35;
      display: flex;
      justify-content: left;
      align-items: center;
    }
    .input{
      width: 343px;
      height: 50px;
      background: #FFFFFF;
      margin-top: 5px;
      border-radius: 20px 20px 20px 20px;
      padding: 0 20px;
      box-sizing: border-box;

      display: flex;
      align-items: center;
      justify-content: space-between;
      .left-phone{
        width: 120px;
        height: 50px;
        display: flex;
        align-items: center;
        margin-left: -10px;
        position: relative;
        .left{
          font-size: 12px;
        }
        img{
          width: 26px;
          height: 26px;
          border-radius: 50%;
          object-fit: contain;
        }

        span{
          margin-left: 5px;
          font-size: 14px;
          color: #242B35;
          b{
            margin-left: 15px;
          }
        }
      }
      .left-nav{
        width: 180px;
        min-height: 100px;
        background: #fff;
        position: absolute;
        left: 0;
        z-index: 99;
        top: 63px;
        border-radius: 10px;
        .jt{
          width: 20px;
          height: 20px;
          background: #fff;
          transform: rotate(45deg);
          top: -5px;
          margin-left: 28px;
          position: absolute;
          border-radius: 5px;
        }
        .menu-box{
          padding:0 10px;
          box-sizing: border-box;
          min-height: 100px;
          max-height: 250px;
          overflow-y: scroll;
          position: relative;
          z-index: 999;
          .box-nav{
            width: 100%;
            height: 36px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #f9f9f9;
            justify-content: space-between;
            span:nth-child(1){
              font-size: 12px;
            }
            span:nth-child(2){
              font-size: 12px;
            }
            img{
              height: 22px;
              width: 22px;
              object-fit: contain;
              border-radius: 50%;
            }
          }
        }

      }
      .left{
        width: 50px;
        height: 50px;
        margin-left: -15px;
        display: flex;
        justify-content: center;
        align-items: center;
        img{
          height: 24px;
          width: 24px;
          object-fit: contain;
        }
      }
      input{
        width: 100%;
        height: 45px;
        background: #FFFFFF;
        border-radius: 20px 20px 20px 20px;
        font-size: 16px;

      }
      input::placeholder{
        font-size: 14px;
        color: rgba(0,0,0,0.3);
      }
      .right-ipt{
        width: 30px;
        height: 50px;
        display: flex;
        justify-content: center;
        align-items: center;
        img{
          width: 24px;
          height: 24px;
          object-fit: contain;
        }
      }
      .right{
        width: 100px;
        height: 40px;
        img{
          width: 100px;
          height: 40px;
          object-fit: contain;
        }
      }
    }
    .error{
      width: 100%;
      font-size: 12px;
      color: #F13D3D;
      margin-top: 5px;
      text-align: right;
    }
  }
  .errors{
    border: 1px solid #F13D3D;
    box-sizing: border-box;
  }
</style>